//
//  ARLoginViewController.m
//  AnimationRecommend
//
//  Created by 花花蔡 on 2017/2/15.
//  Copyright © 2017年 ljm. All rights reserved.
//

#import "ARLoginViewController.h"
#import "EMIconUnderLineTextF.h"
#import "EMSysButton.h"
@interface ARLoginViewController ()

@end

@implementation ARLoginViewController {
    
    CGFloat passwordTextF_Y;
    CGFloat registerBtn_MAX_Y;
    
    EMIconUnderLineTextF *account;
    EMIconUnderLineTextF *password;
    
    UIImage *_image;
    
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    //self.tabBarController.tabBar.hidden = YES;
}

-(void)viewWillDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    //self.tabBarController.tabBar.hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = iCodeTableviewBgColor;
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    [imageView sd_setImageWithURL:[NSURL URLWithString:LoginImageViewUrlStr]];
    [self.view addSubview:imageView];
    
    [self addDelegeteBtn];
    [self addTextFields];
    [self addLoginBtn];
    //[self addOtherLoginBtn];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 加载退出按钮

- (void)addDelegeteBtn {
    
    UIButton *deleteBtn = [[UIButton alloc] initWithFrame:AAdaptionRect(30, 60, 50, 50)];
    [deleteBtn setBackgroundImage:[UIImage imageNamed:@"delete"] forState:normal];
    [deleteBtn addTarget:self action:@selector(dismissVc) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:deleteBtn];
}

#pragma mark - 加载输入框

- (void)addTextFields {
    
    account = [[EMIconUnderLineTextF alloc] initWithFrame:AAdaptionRect(100, 200, 550, 40) withPlaceholder:@"请输入账号" WithFontColor:[UIColor blackColor] WithAnimationType:top];
    account.keyboardType = UIKeyboardTypeNumberPad;
    
    password = [[EMIconUnderLineTextF alloc] initWithFrame:AAdaptionRect(100, 320, 550, 40) withPlaceholder:@"请输入验证码" WithFontColor:[UIColor blackColor] WithAnimationType:bottom];
    password.keyboardType = UIKeyboardTypeNumberPad;
    passwordTextF_Y = CGRectGetMaxY(password.frame);
    
    [self.view addSubview:account];
    [self.view addSubview:password];
    
}


#pragma mark - 加载login按钮
- (void)addLoginBtn {
    
    __weak typeof(self) weakSelf = self;
    
    CGFloat loginBtn_Y = passwordTextF_Y/AAdaptionWidth() + 60/AAdaptionWidth();
    EMSysButton *loginBtn = [[EMSysButton alloc] initCountdownBtnWithFrame:AAdaptionRect(100, loginBtn_Y, 550, 80) WithTitleColor:[UIColor blackColor] WithBKColor:[UIColor whiteColor] WithClickedBlock:^(id sender) {
        
        [weakSelf countDownBtnClicked];
        
    }];
    loginBtn.alpha = 0.7f;
    
    
    CGFloat registerBtn_Y = (CGRectGetMaxY(loginBtn.frame) + 20) / AAdaptionWidth();
    EMSysButton *registerBtn =[[EMSysButton alloc] initWithFrame:AAdaptionRect(100, registerBtn_Y, 550, 80) withTag:1001 withTitle:@"登  录" withTitleColor:[UIColor blackColor] withBackgrougdColor:[UIColor whiteColor] withCornerRadious:0.1 withClickedBlock:^(id sender) {
        //点击了登录
        //        NSLog(@"login");
        [weakSelf btnClicked:sender];
        
    }];
    registerBtn.alpha = 0.7f;
    registerBtn_MAX_Y = CGRectGetMaxY(registerBtn.frame);
    
    [self.view addSubview:loginBtn];
    [self.view addSubview:registerBtn];
}

#pragma mark - 加载三方登录按钮

- (void)addOtherLoginBtn {
    CGFloat otherBtn_Y = registerBtn_MAX_Y/AAdaptionWidth() + 30/AAdaptionWidth();
    
    __weak typeof(self) weakSelf = self;
    
    NSArray *btnName = @[@"QQ",@"WeiXin",@"WeiBo"];
    
    for (int index = 0; index < 3; index ++) {
            EMSysButton *btn = [[EMSysButton alloc] initWithFrame:AAdaptionRect(210 + index*140, otherBtn_Y , 70, 70) withImgName:btnName[index] WithTag:200 + index WithCornerRadious:0.5f withClickedBlock:^(id sender) {
                
                [weakSelf btnClicked:sender];
            }];
        [self.view addSubview:btn];
    }
}


#pragma mark - 退出按钮点击事件
- (void)dismissVc {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)btnClicked:(UIButton *)btn {
    
    switch (btn.tag) {
        case 1001:
            //登录
        {
            [self cloudLogin];
        
        }
            break;
        case 200:
            //QQ登录
        case 201:
            //微信
            break;
        case 202:
            //微博
            break;
        default:
            break;
    }
}


#pragma mark - 登录注册的相关逻辑判断

- (void)cloudLogin {
    
    __weak typeof(self) weakSelf = self;
    [AVUser signUpOrLoginWithMobilePhoneNumberInBackground:account.text smsCode:password.text block:^(AVUser *user, NSError *error) {
        // 如果 error 为空就可以表示登录成功了，并且 user 是一个全新的用户
        if (error != nil) {
            NSLog(@"注册失败");
           [CLLMessageBarTool showMessageWithType:TWMessageBarMessageTypeError WithTitle:@"提示" WithDescription:@"登录失败"];
        }else {
            [CLLMessageBarTool showMessageWithType:TWMessageBarMessageTypeSuccess WithTitle:@"提示" WithDescription:@"登录成功"];
            [weakSelf saveUserInfoWithAVUser:user];
            [weakSelf dismissViewControllerAnimated:YES completion:nil];
        }
    }];
    

}

- (void)countDownBtnClicked {
    NSLog(@"点了倒计时btn");
    if (![XXCheckManager validateMobile:account.text] || [account.text isEqualToString:@""] ) {
        [[CLLScottAlertView alloc] showOnWindowWithTitle:@"逗我" WithMessage:@"20s惩罚时间"];
        return;
    }

    //号码正确后发送验证码
    [AVOSCloud requestSmsCodeWithPhoneNumber:account.text callback:^(BOOL succeeded, NSError *error) {
        // 发送失败可以查看 error 里面提供的信息
        if (error == nil) {
            [CLLMessageBarTool showMessageWithType:TWMessageBarMessageTypeSuccess WithTitle:@"提示" WithDescription:@"发送成功"];
            
        }else {
            [CLLMessageBarTool showMessageWithType:TWMessageBarMessageTypeError WithTitle:@"失败" WithDescription:@"请等待20秒"];
        }
    }];
}

#pragma mark - save user info 
- (void)saveUserInfoWithAVUser:(AVUser *)user {
    
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:isLoginKey];
    
     [[NSNotificationCenter defaultCenter] postNotificationName:@"iconImageLogin" object:nil userInfo:nil];
}

@end
